#!/usr/bin/perl 

use strict;
use DBI;
use CGI;
use TTraker;

my $dbh;
& initialize_dbi;
& print_header;

my $form_ref = parse_input();
my %FORM_DATA = %$form_ref; 
my $empNum = $FORM_DATA{'empNum'};
my $empName = $FORM_DATA{'empName'};
my $empJob = $FORM_DATA{'empJob'};
my $empLoc = $FORM_DATA{'empLoc'};
my $activity = $FORM_DATA{'empAct'};
my $batNum = $FORM_DATA{'batNum'};
my @holds =();
foreach my $key (keys %FORM_DATA) {
	if ($key =~ /^HOLDID_/ ) {
		my $hold = $key;
		$hold =~ s/HOLDID_//;
		push @holds , $hold;
		add_hold_to_batch ($hold,$batNum);
	}
}	

# foreach my $h (@holds) {
# 	print "$h<br/>";
# }
log_tasks ($empNum, $empName, $empJob, $empLoc, $batNum, \@holds, $activity); 

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

sub log_tasks
{
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $batch = $_[4];
	my $holds_ref = $_[5];
	my $activity =  $_[6];
	my $now =  get_local_time($loc);
	
	my @holds = @$holds_ref;
	
	my $code = "TASKS";
	start_system_time ($num, $code, $now);

	my ($warning, $task_count, $task_ref, $doc, $job, $task, $emp, $cli, $stmp);

	my $job_count = get_open_job_count ($num);

	if ( $job_count == 0 ) {
		$warning = "NO OPEN JOB FOR $name! Can not log tasks.";
		activity_screen_from_pm ($num, $name, $job_id, $loc, $warning);
		# trak_02_activity_selection_message ($num, $name, $job_id, $loc,8, $warning);
	} elsif ( $job_count == 1 ) {
		my $job_ref = get_open_job ($num);
		($doc,$job,$emp,$cli,$stmp) = @$job_ref;
		if ($cli eq "LUNCH" || $cli eq "BREAK" || $cli eq "IDLE") {
      		$warning = "CURRENT ACTIVITY: $cli! Can not log tasks!";
				activity_screen_from_pm ($num, $name, $job_id, $loc, $warning);
      } else {
				
      		$task_count = get_task_count ($cli, $job_id, "inbound");
      		if ($task_count > 0 ) {
  					add_hold_to_batch ($job,$batNum);
      	  		& trak_02_log_tasks_screen ($num, $name, $job_id, $loc, $cli,$stmp, $doc, $job, $activity, $batch);
      		} else {
	      		$warning = "NO TASKS FOUND FOR $cli! See Administrator!";
					activity_screen_from_pm ($num, $name, $job_id, $loc, $warning);
      			# do nadda
      		}
      }
	} else {
		# DO NADDA
	}	
# print "WE ARE DONE!<br/>";
}

sub trak_02_log_tasks_screen
{
#	print "OLA!<br/>";
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $cli = $_[4];
	my $stmp = $_[5];
	my $doc = $_[6];
	my $job = $_[7];
	my $activity = $_[8];
	my $batch = $_[9];
	
#	my $task = $_[8];

	my $button_text;
	if ( $activity == 8 ) {
		$button_text = "Log Tasks - close jobs";
	} elsif ($activity == 3 ) {
		$button_text = "Log Tasks - keep job open";
	}	else {
		$button_text = "Log Tasks - ????";
	} 
	my $task_ref = get_task_list ($cli, $job_id, "outbound");
 	my @tasks = @$task_ref;
	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_activity_log_tasks.pl</title>
	</head>
	<body onload='startTime()'>
	<h2><center>${banner}</center></h2>
	<hr/>
	<div id='clock'></div>
	<div id='box1'>
	Employee Number:<br/>
	Employee Name:<br/>
	Client:<br/>
	Document ID:<br/>
	</div>
	<div id='box2'>
	<span>$num</span><br/>
	<span>$name</span><br/>
	<span>$cli</span><br/>
	<span>$doc</span><br/><br/>
	</div>
	<form autocomplete='off' name='select_area' action='${cgi_ref}trak_05_log_tasks_requery.pl' method='post'>
	<div id="box1_2">
	<input type="hidden" name="empNum" value='$num'/>
	<input type="hidden" name="empName" value='$name'/>
	<input type="hidden" name="empJob" value='$job_id'/>
	<input type="hidden" name="empLoc" value='$loc'/>
	<input type="hidden" name=docNum value='$doc'/>
	<input type="hidden" name=jobNum value='$job'/>
	<input type="hidden" name=cliId value='$cli'/>
	<input type="hidden" name=stamp value='$stmp'/>
	<input type="hidden" name="activity" value='$activity'/>
	<input type="hidden" name="batNum" value='$batch'/>
	<label><input type="radio" name="requery" value="inbound" />&nbsp&nbsp&nbsp Inbound</label>
	<label><input type="radio" name="requery" value="outbound" checked="checked" />&nbsp&nbsp&nbsp Outbound </label>
	<label><input type="radio" name="requery" value="include" />&nbsp&nbsp&nbsp All  &nbsp&nbsp&nbsp</label>
	<input type='submit' value='Select'>
	<br/>
	</div>        
	</form>
	<div id='box5'>
	<form onkeypress='return event.keyCode != 13;' autocomplete='off'  name='activity' action='${cgi_ref}trak_05_log_tasks.pl' method='post'>
	<input type="hidden" name="empNum" value='$num'/>
	<input type="hidden" name="empName" value='$name'/>
	<input type="hidden" name="empJob" value='$job_id'/>
	<input type="hidden" name="empLoc" value='$loc'/>
	<input type="hidden" name=docNum value='$doc'/>
	<input type="hidden" name=jobNum value='$job'/>
	<input type="hidden" name=cliId value='$cli'/>
	<input type="hidden" name=stamp value='$stmp'/>
	<input type="hidden" name="activity" value='$activity'/>
	<input type="hidden" name="batNum" value='$batch'/>
	<table>
	<tr>
		<th id='th_right'>Activity</th>
		<th></th>
		<th>Units</th>
	</tr>
ENDHEADER
	for my $i (0..$#tasks ) {
		my $code=$tasks[$i][0];
		my $desc = $tasks[$i][1];
		$desc =~ tr/A-Z/a-z/;
		my $units = $tasks[$i][2];
		$units =~ tr/A-Z/a-z/;
print <<ENDHEADER;
		<tr>
		<td id='description1' >${desc}&nbsp&nbsp </td>
		<td><input type='text' size='4' name='task_${code}'/></td>
		<td>&nbsp&nbsp&nbsp $units</td>
		</tr>
ENDHEADER
	}
	print <<ENDHEADER;
	</table>
	<br/><br/>
	<input type='submit' value='$button_text'>
	<br/><br/>
	&nbsp&nbsp&nbsp&nbsp&nbsp
	<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
   </form>
   </div>
   </body>
   </html>
ENDHEADER
}
